<template>
    <view class="coreshop-share-Box">
        <u-toast ref="uToast" />
        <view class="coreshop-share-pop">
            <view class="coreshop-share-item">
                <button class="coreshop-btn" open-type="share">
                    <u-icon label="分享微信好友" labelPos="bottom" size="40" name="/static/images/common/share-friend.png"></u-icon>
                </button>
            </view>
            <view class="coreshop-share-item">
                <button class="coreshop-btn" @click="$u.throttle(createPoster, 500)">
                    <u-icon label="生成海报" labelPos="bottom" size="40" name="/static/images/common/share-poster.png"></u-icon>
                </button>
            </view>
        </view>
        <view class="coreshop-share-bottomBox">
            <button class="coreshop-btn coreshop-btn-w coreshop-btn-square" @click="close()">关闭</button>
        </view>
    </view>
</template>

<script>
    export default {
        props: {
            // 通用传参id
            objectId: {
                type: Number,
                default: 0
            },
            // 商品id
            goodsId: {
                type: Number,
                default: 0
            },
            // 分享的图片
            shareImg: {
                type: String,
                default: ''
            },
            // 分享标题
            shareTitle: {
                type: String,
                default: ''
            },
            // 分享内容
            shareContent: {
                type: String,
                default: ''
            },
            // 分享链接
            shareHref: {
                type: String,
                default: ''
            },
            //分享类型
            shareType: {
                type: Number,
                default: 1
            },
            //拼团Id/团购Id/秒杀Id
            groupId: {
                type: Number,
                default: 0
            },
            //拼团的团队id
            teamId: {
                type: Number,
                default: 0
            }
        },
        data() {
            return {
                providerList: [] // 分享通道 包含生成海报
            }
        },
        mounted() {
            //    console.log("类型：" + this.shareType);
        },
        methods: {
            // 关闭弹出层
            close() {
                this.$emit('close')
            },
            // 生成海报
            createPoster() {
                uni.showLoading({
                    title: '加载中',
                    mask: true
                });
                this.close()
                let data = {};
                if (this.shareType == this.$globalConstVars.shareType.index) {
                    data = {
                        page: this.shareType, //商品
                        url: this.$globalConstVars.shareUrl,
                        params: {
                            goodsId: this.goodsId
                        },
                        type: this.$globalConstVars.shareModel.poster,
                        client: this.$globalConstVars.shareClient.wxMiNiProgram
                    }
                    let userToken = this.$db.get('userToken')
                    if (userToken) {
                        data.token = userToken
                    }
                } else if (this.shareType == this.$globalConstVars.shareType.pinTuan) {
                    data = {
                        page: this.shareType,
                        url: this.$globalConstVars.shareUrl,
                        params: {
                            groupId: this.groupId,
                            goodsId: this.goodsId,
                            teamId: this.teamId
                        },
                        type: this.$globalConstVars.shareModel.poster,
                        client: this.$globalConstVars.shareClient.wxMiNiProgram
                    }
                    let userToken = this.$db.get('userToken')
                    if (userToken) {
                        data.token = userToken
                    }
                }
                else if (this.shareType == this.$globalConstVars.shareType.addPinTuan) {
                    data = {
                        page: this.shareType, //拼团
                        url: this.$globalConstVars.shareUrl,
                        params: {
                            goodsId: this.goodsId,
                            groupId: this.groupId,
                            teamId: this.teamId
                        },
                        type: this.$globalConstVars.shareModel.poster,
                        client: this.$globalConstVars.shareClient.wxMiNiProgram
                    }
                    let userToken = this.$db.get('userToken')
                    if (userToken) {
                        data.token = userToken
                    }
                }
                else if (this.shareType == this.$globalConstVars.shareType.group) {
                    data = {
                        page: this.shareType,
                        url: this.$globalConstVars.shareUrl,
                        params: {
                            groupId: this.groupId
                        },
                        type: this.$globalConstVars.shareModel.poster,
                        client: this.$globalConstVars.shareClient.wxMiNiProgram
                    }
                    let userToken = this.$db.get('userToken')
                    if (userToken) {
                        data.token = userToken
                    }
                }
                else if (this.shareType == this.$globalConstVars.shareType.seckill) {
                    data = {
                        page: this.shareType,
                        url: this.$globalConstVars.shareUrl,
                        params: {
                            groupId: this.groupId
                        },
                        type: this.$globalConstVars.shareModel.poster,
                        client: this.$globalConstVars.shareClient.wxMiNiProgram
                    }
                    let userToken = this.$db.get('userToken')
                    if (userToken) {
                        data.token = userToken
                    }
                }
                else if (this.shareType == this.$globalConstVars.shareType.goods) {
                    data = {
                        page: this.shareType, //拼团
                        url: this.$globalConstVars.shareUrl,
                        params: {
                            goodsId: this.goodsId,
                        },
                        type: this.$globalConstVars.shareModel.poster,
                        client: this.$globalConstVars.shareClient.wxMiNiProgram
                    }
                    let userToken = this.$db.get('userToken')
                    if (userToken) {
                        data.token = userToken
                    }
                }
                else if (this.shareType == this.$globalConstVars.shareType.solitaire) {
                    data = {
                        page: this.shareType,
                        url: this.$globalConstVars.shareUrl,
                        params: {
                            id: this.objectId
                        },
                        type: this.$globalConstVars.shareModel.poster,
                        client: this.$globalConstVars.shareClient.wxMiNiProgram
                    }
                    let userToken = this.$db.get('userToken')
                    if (userToken) {
                        data.token = userToken
                    }
                } else if (this.shareType == this.$globalConstVars.shareType.serviceGoods) {
                    data = {
                        page: this.shareType,
                        url: this.$globalConstVars.shareUrl,
                        params: {
                            serviceGoodsId: this.objectId
                        },
                        type: this.$globalConstVars.shareModel.poster,
                        client: this.$globalConstVars.shareClient.wxMiNiProgram
                    }
                    let userToken = this.$db.get('userToken')
                    if (userToken) {
                        data.token = userToken
                    }
                }
                this.$u.api.share(data).then(res => {
                    if (res.status) {
                        uni.hideLoading();
                        if (
                            this.shareType == this.$globalConstVars.shareType.goods ||
                            this.shareType == this.$globalConstVars.shareType.pinTuan ||
                            this.shareType == this.$globalConstVars.shareType.addPinTuan ||
                            this.shareType == this.$globalConstVars.shareType.seckill ||
                            this.shareType == this.$globalConstVars.shareType.group ||
                            this.shareType == this.$globalConstVars.shareType.serviceGoods
                        ) {
                            //data.posterUrl = encodeURIComponent(res.data);
                            data.params.posterUrl = res.data;
                            data.params.shareType = this.shareType;

                            this.$u.route('/pages/share/shareNewPoster/shareNewPoster' + uni.$u.queryParams(data.params))
                        } else {
                            this.$u.route('/pages/share/sharePoster/sharePoster?poster=' + encodeURIComponent(res.data))
                        }
                    } else {
                        uni.hideLoading();
                        this.$u.toast(res.msg)
                    }
                });
            }
        }
    }
</script>
